import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hc_bot_pro/common/tool/string_ex.dart';
import 'package:hc_cure_sdk_flutter/model/cure/cure.dart';

import '../common/routes.dart';
import '../common/service/therapy_service.dart';
import '../common/ui/qr.dart';
import 'index.dart';
import 'state/state.dart';
import 'widgets/background.dart';

class PlayerPage extends GetView<PlayerController> {
  const PlayerPage({super.key});

  // 主视图
  Widget _buildView() {
    return Center(
      child: Text(
        controller.state.timePositionDesc,
        style: TextStyle(
          fontSize: 28.sp,
          color: controller.channel == null
              ? Colors.black
              : controller.channel!.appStyle.colors.playThemeColor.toColor(),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final cureID = Get.arguments["schemeId"];
    final cure = Get.find<TherapyService>().pickCure(cureID);
    assert(cure != null);

    final duration = Get.arguments["duration"] ?? 0;
    final title = Get.arguments["subject"] ?? "";
    final state = CurePlayerState(
      cure: cure!,
      duration: Duration(minutes: duration),
      title: title,
    );

    return BackgroundView(
      tip: cure.tip,
      child: GetBuilder<PlayerController>(
        init: PlayerController(state),
        id: "player",
        builder: (_) {
          return PopScope(
            canPop: controller.state.isMusicStandby,
            onPopInvoked: (didPop) {
              if (didPop) {
                controller.stop();
              }
            },
            child: Theme(
              data: ThemeData(
                appBarTheme: AppBarTheme(
                  centerTitle: true,
                  color: Colors.transparent,
                  iconTheme: IconThemeData(
                    color: controller.channel == null
                        ? Colors.black
                        : controller.channel!.appStyle.colors.pickTitleColor
                              .toColor(), // 设置返回箭头颜色为白色
                  ),
                  titleTextStyle: TextStyle(
                    fontSize: 15.sp,
                    color: controller.channel == null
                        ? Colors.black
                        : controller.channel!.appStyle.colors.pickTitleColor
                              .toColor(),
                  ),
                ),
              ),
              child: Scaffold(
                backgroundColor: Colors.transparent,
                appBar: AppBar(title: Text(controller.state.title)),
                floatingActionButton: QrPanel(),
                body: SafeArea(child: _buildView()),
              ),
            ),
          );
        },
      ),
    );
  }
}
